package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @FileName: HospitalSetController
 * @Author Steven
 * @Date: 2021/3/14
 */
@Api(tags = "医院管理设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * @return 查询医院所有信息
     */
    @ApiOperation("查询所有的医院信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list(null);
        return Result.ok(list);
    }

    @ApiOperation("根据ID删除医院信息")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * @param current 当前页
     * @param limit 每页显示的条数
     * @param hospitalSetQueryVo 查询条件
     * @return 医院信息
     */
    @ApiOperation("分页查询医院信息")
    @PostMapping("/{current}/{limit}")
    public Result findHospPage(@PathVariable int current,
                               @PathVariable int limit,
                               @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page=new Page<>(current,limit);
        QueryWrapper<HospitalSet> queryWrapper=new QueryWrapper<>();
        if(null!=hospitalSetQueryVo.getHosname()){
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }if(null!=hospitalSetQueryVo.getHoscode()){
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        Page pageHospitalSet = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageHospitalSet);
    }


    /**
     * @param hospitalSet 添加医院信息
     * @return 添加医院是否成功信息
     */
    @ApiOperation("添加医院信息")
    @PostMapping("/saveHospital")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        //配置签名钥匙
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag){
           return Result.ok();
        }else{
            return Result.fail();
        }

    }

    @ApiOperation("更新医院信息")
    @PostMapping("/updateHospital")
    public Result updateHospital(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * @param id 医院ID
     * @return 医院信息
     */
    @ApiOperation("根据ID查找医院信息")
    @GetMapping("/getHospital/{id}")
    public Result findHostpitalById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation("根据ID批量删除医院信息")
    @DeleteMapping("/batchRemove")
    public Result deleteHospitals(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if(flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * @param id 医院ID
     * @param status 是否被锁定
     * @return 返回结果
     */
    @PutMapping("/{id}/{status}")
    public Result lockHospital(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * @param id 医院ID
     * @return 根据ID返回密钥信息
     */
    @ApiOperation("获取密钥")
    @GetMapping("/send/{id}")
    public Result sendMessage(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.getHosname();
        hospitalSet.getSignKey();
        return Result.ok();
    }

 }
